import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useProjectStore = defineStore('project', () => {
  // 模拟项目数据
  const projects = ref([
    {
      id: 1,
      name: '园区A道路建设',
      startDate: '2024-01-01',
      endDate: '2024-06-30',
      progress: 65,
      status: '进行中',
      manager: '张三',
      budget: {
        initial: 5000000,
        actual: 3200000,
        adjustments: []
      },
      logs: [
        {
          id: 1,
          date: '2024-03-10',
          activity: '地基施工完成',
          issues: '材料延迟到货',
          resolved: true
        }
      ]
    },
    {
      id: 1,
      name: '园区B道路建设',
      startDate: '2024-01-01',
      endDate: '2024-06-30',
      progress: 50,
      status: '进行中',
      budget: {
        initial: 5000000,
        actual: 3200000,
        adjustments: []
      },
      logs: [
        {
          id: 1,
          date: '2024-03-10',
          activity: '地基施工完成',
          issues: '材料延迟到货',
          resolved: true
        }
      ]
    },
    {
      id: 1,
      name: '园区C道路建设',
      startDate: '2024-01-01',
      endDate: '2024-06-30',
      progress: 78,
      status: '进行中',
      budget: {
        initial: 5000000,
        actual: 3200000,
        adjustments: []
      },
      logs: [
        {
          id: 1,
          date: '2024-03-10',
          activity: '地基施工完成',
          issues: '材料延迟到货',
          resolved: true
        }
      ]
    },
    {
      id: 1,
      name: '园区D道路建设',
      startDate: '2024-01-01',
      endDate: '2024-06-30',
      progress: 85,
      status: '进行中',
      budget: {
        initial: 5000000,
        actual: 3200000,
        adjustments: []
      },
      logs: [
        {
          id: 1,
          date: '2024-03-10',
          activity: '地基施工完成',
          issues: '材料延迟到货',
          resolved: true
        }
      ]
    }
  ])
  // 巡检相关数据
  const inspections = ref([])
  const issues = ref([])

  // 获取分页项目
  const paginatedProjects = computed(() => (page, size) => {
    const start = (page - 1) * size
    return projects.value.slice(start, start + size)
  })

  // 巡检任务方法
  const createInspection = (projectId, task) => {
    inspections.value.push({
      id: Date.now(),
      projectId,
      ...task,
      status: '待分配',
      records: [],
      createdAt: new Date().toISOString()
    })
  }

  // 问题上报方法
  const reportIssue = (projectId, issue) => {
    issues.value.push({
      id: Date.now(),
      projectId,
      ...issue,
      status: '待处理',
      createdAt: new Date().toISOString()
    })
  }
  // 计算属性：预算偏差
  const budgetVariance = computed(() => (project) => {
    return project.budget.actual - project.budget.initial
  })

  // 更新进度
  const updateProgress = (projectId, newProgress) => {
    const project = projects.value.find((p) => p.id === projectId)
    if (project) {
      project.progress = newProgress
      project.status = newProgress >= 100 ? '已完成' : '进行中'
    }
  }

  // 添加施工日志
  const addLog = (projectId, log) => {
    const project = projects.value.find((p) => p.id === projectId)
    if (project) {
      project.logs.unshift({
        ...log,
        id: Date.now(),
        date: new Date().toISOString().split('T')[0]
      })
    }
  }

  // 调整预算
  const adjustBudget = (projectId, adjustment) => {
    const project = projects.value.find((p) => p.id === projectId)
    if (project) {
      project.budget.adjustments.push(adjustment)
      project.budget.initial += adjustment.amount
    }
  }

  return {
    projects,
    budgetVariance,
    updateProgress,
    addLog,
    adjustBudget,
    inspections,
    issues,
    paginatedProjects,
    createInspection,
    reportIssue
  }
})
